<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

    //Start Index
	public function index()
	{
	   if($this->session->userdata('logged_in')) 
	   {
	    $data = array(
               'pageTitle' => 'Administrator',
               'heading' => 'please Login',
               'errorMessage' => ''
           );
	      $this->load->view('administrator/index',$data);
	   }
	   else	  
	    {
		 $data = array(
               'title' => 'Login',
               'heading' => 'please Login',
               'errorMessage' => ''
           );
		 $this->load->view('administrator/login',$data);
		}
	}///End Index
	
	 //Start Login
	public function login($user = '', $password = '')
	{
	   $this->load->library('admin_ecom');
	   
	   $this->admin_ecom->doLogin($this->input->post('txtUserName'), $this->input->post('txtPassword'),'127.0.0.1');
	  
	   if(!$this->session->userdata('logged_in')) {
		 $data = array(
               'title' => 'Login',
               'heading' => 'please Login',
               'errorMessage' => 'Wrong user name password'
           );
		   $this->load->view('administrator/login',$data);
		}
		else {
	   redirect('admin');
	   }
		
	}///End
	
	
	function logout() {
        $this->CI =& get_instance();        
        $this->CI->session->sess_destroy();
		redirect('admin');
    }
	
	/////////////////////////////////////////////////
	function category($view='',$catId='')
	 {
	  
	   if(!$this->session->userdata('logged_in')){
	    $this->load->view('administrator/login',$data['errorMessage']="Please Login");
		return;
		}
		
		$this->load->library('admin_ecom');
		switch ($view) {
			case 'list' :
				$query=$this->admin_ecom->getCategoryList($catId);
				$data = array('query' => $query,'pageTitle'=>'Category List');
				$this->load->view('administrator/include/header',$data);
				$this->load->view('administrator/include/sidebar',$data);
				$this->load->view('administrator/category/list',$data);
				$this->load->view('administrator/include/footer',$data);
				break;
		
			case 'add' :
				$data = array('pageTitle'=>'Add Category','parentId'=>$catId);
				$this->load->view('administrator/include/header',$data);
				$this->load->view('administrator/include/sidebar',$data);
				$this->load->view('administrator/category/add',$data);
				$this->load->view('administrator/include/footer',$data);
				break;
		
			case 'modify' :
				$query=$this->admin_ecom->getModifyCategory($catId);
				$data = array('query' => $query,'pageTitle'=>'Category List');
				$this->load->view('administrator/include/header',$data);
				$this->load->view('administrator/include/sidebar',$data);
				$this->load->view('administrator/category/modify',$data);
				$this->load->view('administrator/include/footer',$data);
				break;
		
			default :
				$content 	= 'list.php';		
				$pageTitle 	= 'Shop Admin Control Panel - View Category';
		}
		

	 } //End Catagory
	 //////////////////////////////////////////////////////
   function extentionCategory($action=''){
       if(!$this->session->userdata('logged_in')){
	    $this->load->view('administrator/login',$data['errorMessage']="Please Login");
		return;
		}
		$this->load->library('processcategory');
		$action=$this->input->post('action');
		/*$url = parse_url($_SERVER['REQUEST_URI']);
		$atl=$url['query'];*/
		switch ($action) {
			case 'add' :
	             $name=$this->input->post('txtName');
				 $description=$this->input->post('mtxDescription');
				 $image=$_FILES['fleImage'];
				 $parentId=$this->input->post('hidParentId');
				 $query=$this->processcategory->addCategory($name,$description,$image,$parentId);
				 redirect('admin/category/list');
				break;	  
			case 'modify' :
			     $catId=$this->input->get('catId');
	             $txtName=$this->input->post('txtName');
				 $mtxDescription=$this->input->post('mtxDescription');
				 $fleImage=$_FILES['fleImage'];
				 $query=$this->processcategory->modifyCategory( $catId,$txtName,$mtxDescription,$fleImage);
				 redirect('admin/category/list');
				break;
			case 'delete' :
				deleteCategory();
				break;
			case 'deleteImage' :
				deleteImage();
				break;
			default :
				 redirect('admin');
		   }
     }//End ModifyCategory
	 
	 
	 /////////////////////////////////////////////////
	 
	 
	 /////////////////////////////////////////////////
	function product($view='',$productId='')
	 {
	  
	   if(!$this->session->userdata('logged_in')){
	    $this->load->view('administrator/login',$data['errorMessage']="Please Login");
		return;
		}
		
		$this->load->library('admin_ecom');
		switch ($view) {
			case 'list' :
			    $catId=$this->input->get('catId');
				$query=$this->admin_ecom->getProductList($catId);
				$catList=$this->admin_ecom->getCategoryList();
				$data = array('query' => $query,'categoryList'=>$catList,'pageTitle'=>'Product List','catId'=>$catId);
				$this->load->view('administrator/include/header',$data);
				$this->load->view('administrator/include/sidebar',$data);
				$this->load->view('administrator/product/list',$data);
				$this->load->view('administrator/include/footer',$data);
				break;
		
			case 'add' :
				$catList=$this->admin_ecom->getCategoryList();
				$data = array('categoryList'=>$catList,'pageTitle'=>'Product List');
				$this->load->view('administrator/include/header',$data);
				$this->load->view('administrator/include/sidebar',$data);
				$this->load->view('administrator/product/add',$data);
				$this->load->view('administrator/include/footer',$data);
				break;
		
			case 'modify' :
			    $catId=$this->input->get('catId');
				$query=$this->admin_ecom->getModifyProduct($productId);
				$catList=$this->admin_ecom->getCategoryList();
				$data = array('query' => $query,'pageTitle'=>'Modify Product','categoryList'=>$catList,'productId'=>$productId);
				$this->load->view('administrator/include/header',$data);
				$this->load->view('administrator/include/sidebar',$data);
				$this->load->view('administrator/product/modify',$data);
				$this->load->view('administrator/include/footer',$data);
				break;
		
			default :
				$content 	= 'list.php';		
				$pageTitle 	= 'Shop Admin Control Panel - View Category';
		}
		

	 } //End Catagory
	 ///////////////////////////////////////////////////////////
	 	 //////////////////////////////////////////////////////
   function extentionProduct($action=''){
       if(!$this->session->userdata('logged_in')){
	    $this->load->view('administrator/login',$data['errorMessage']="Please Login");
		return;
		}
		$this->load->library('processproduct');
		$action=$this->input->post('action');
		/*$url = parse_url($_SERVER['REQUEST_URI']);
		$atl=$url['query'];*/
		switch ($action) {
			case 'addProduct' :
				 $image=$_FILES['fleImage'];
				 $catId       = $this->input->post('cboCategory');
				 $name        = $this->input->post('txtName');
				 $description = $this->input->post('mtxDescription');
				 $price       = str_replace(',', '', (double)$this->input->post('txtPrice'));
				 $qty         = (int)$this->input->post('txtQty');
				 $query=$this->processproduct->addProduct($catId ,$name,$description,$price,$qty ,$image);
				 redirect('admin/product/list');
				break;	  
			case 'modify' :
			     $productId=$this->input->get('productId');
				 $catId=$this->input->post('cboCategory');
	             $txtName=$this->input->post('txtName');
				 $mtxDescription=$this->input->post('mtxDescription');
				 $txtPrice=$this->input->post('txtPrice');
				 $txtQty=$this->input->post('txtQty');
				 $fleImage=$_FILES['fleImage'];
				 $query=$this->processproduct->modifyProduct($productId,$catId,$txtName,$mtxDescription,$txtPrice,$txtQty,$fleImage);
				 redirect('admin/product/list');
				break;
			case 'delete' :
				deleteCategory();
				break;
			case 'deleteImage' :
				deleteImage();
				break;
			default :
				 redirect('admin');
		   }
     }//End ModifyCategory
	 ///////////////////////////////////////////////////////////
	 
}
